﻿using HlElock.Api;
using HlElock.Api.Models;
using HlELock.Page.Views;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace HlELock.Page.ViewModels
{
    public class AddRoomViewModel : BindableBase
    {
        private readonly RoomApiClient _apiClient;
        private readonly Action<RoomInfo> _onRoomAdded; // 回调函数，用于将新房间添加到主列表

        // 用于添加的表单数据
        private RoomInfo _newRoomForm = new RoomInfo();
        public RoomInfo NewRoomForm
        {
            get => _newRoomForm;
            set => SetProperty(ref _newRoomForm, value);
        }

        // 添加命令
        public DelegateCommand AddRoomCommand { get; }

        public AddRoomViewModel(RoomApiClient apiClient, Action<RoomInfo> onRoomAdded)
        {
            _apiClient = apiClient ?? throw new ArgumentNullException(nameof(apiClient));
            _onRoomAdded = onRoomAdded ?? throw new ArgumentNullException(nameof(onRoomAdded));

            AddRoomCommand = new DelegateCommand(async () => await AddRoomAsync());
        }

        private async Task AddRoomAsync()
        {
            try
            {
                var roomToAdd = new RoomInfo
                {
                    FlType = NewRoomForm.FlType,
                    TjDate = NewRoomForm.TjDate,
                    ExtNum = NewRoomForm.ExtNum,
                    OperId = NewRoomForm.OperId,
                    Floor = NewRoomForm.Floor,
                    Build = NewRoomForm.Build,
                    Window = NewRoomForm.Window,
                    PrjCode = NewRoomForm.PrjCode,
                    RoomId = NewRoomForm.RoomId,
                    Face = NewRoomForm.Face,
                    PreId = NewRoomForm.PreId,
                    SufId = NewRoomForm.SufId
                };

                var apiRequest = new RoomInfo
                {
                    FlType = roomToAdd.FlType,
                    TjDate = roomToAdd.TjDate,
                    ExtNum = roomToAdd.ExtNum,
                    OperId = roomToAdd.OperId,
                    Floor = roomToAdd.Floor,
                    Build = roomToAdd.Build,
                    Window = roomToAdd.Window,
                    PrjCode = roomToAdd.PrjCode,
                    RoomId = roomToAdd.RoomId,
                    Face = roomToAdd.Face,
                    PreId = roomToAdd.PreId,
                    SufId = roomToAdd.SufId
                };

                bool success = await _apiClient.AddRoomAsync(apiRequest);
                if (success)
                {
                    _onRoomAdded(roomToAdd); // 通过回调将新房间添加到主列表
                    MessageBox.Show("房间添加成功！", "成功", MessageBoxButton.OK, MessageBoxImage.Information);
                    Application.Current.Windows.OfType<AddRoomWindow>().FirstOrDefault()?.Close();
                }
                else
                {
                    MessageBox.Show("添加房间失败，请检查网络或输入信息。", "失败", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"添加房间时发生错误: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private bool CanAddOrEdit()
        {
            return !string.IsNullOrWhiteSpace(NewRoomForm.RoomId) &&
                   !string.IsNullOrWhiteSpace(NewRoomForm.FlType) &&
                   !string.IsNullOrWhiteSpace(NewRoomForm.Floor) &&
                   !string.IsNullOrWhiteSpace(NewRoomForm.Build) &&
                   !string.IsNullOrWhiteSpace(NewRoomForm.TjDate) &&
                   !string.IsNullOrWhiteSpace(NewRoomForm.OperId);
        }
    }
}
